# -*- coding: utf-8 -*-

from urllib import request, parse
import json

host = 'http://freecityid.market.alicloudapi.com'
path = '/whapi/json/alicityweather/briefforecast3days'
method = 'POST'
appcode = '87fdc3bdc7a6437f920d26ddba37dfa6'
querys = ''
bodys = {}
url = host + path

bodys['cityId'] = '''12'''
bodys['token'] = '''46e13b7aab9bb77ee3358c3b672a2ae4'''
post_data = parse.urlencode(bodys)
r = request.Request(url)
r.add_header('Authorization', 'APPCODE ' + appcode)
r.add_header('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')

with request.urlopen(r, data=post_data.encode('UTF-8')) as f:
    print('Status:', f.status)
    for k, v in f.getheaders():
        print('%s : %s' % (k, v))
    dat = f.read().decode('UTF-8')
    j = json.loads(dat)
    print(j['data']['condition']['updatetime'], '  ', j['data']['city']['name'], '  摄氏', j['data']['condition']['temp'])
